Scrolling to a Line
Liberty BASIC allows us to scroll the texteditor so that a specified row and column of text appears in the upper left corner. That corner is called the ORIGIN . The syntax for the !ORIGIN command is as follows in LB4.01:
#1.texteditor "!origin row column"
In LB4.02, the column receiver variable comes first:
#1.texteditor "!origin column row"
To scroll to line 10, character 2, the code looks like this:
#1.texteditor "!origin 10 2"
or for LB4.02:
#1.texteditor "!origin 2 10"
To do the same thing with variables, place the variables outside of the quotation marks and preserve the blank spaces, like this:
row = 10 : col = 2
#1.texteditor "!origin ";row;" ";col
or for LB4.02:
row = 10 : col = 2
#1.texteditor "!origin ";col;" ";row
This command makes it very easy to create a "Go to Line" tool. Eddie adds a Tools Menu for version 4, and it includes this handy tool.
Which Line?
We must ask the user which line he wants to go to. We'll do that with a PROMPT message. See the Tip Corner - Prompt for more on this command. Here is some code that asks the user which line he'd like to access:
prompt "Which line?";gotoLine
After the user dismisses the prompt, the program can scroll to the chosen line.
Evaluating Input
What if the user selects a line number that is greater than the number of lines in the texteditor? To trap that error, we check the number of lines and place it into the variable "rowcount".
blah blah validate input
#1.te "!lines rowcount"
if gotoLine>rowcount then gotoLine=rowcount
if gotoLine<1 then gotoLine=1
If the number is greater than the total number of lines, we change it so that it scrolls to the last line. If the number is less than 1, we change it to 1. Now that we've validated the value in "gotoLine" we can scroll to that line using the "!origin" command. Because we are using a variable in the command, we place it outside the quotation marks and preserve the blank spaces, like this:
#1.te "!origin ";gotoLine;" 1"
or for LB4.02:
#1.te "!origin 1 ";gotoLine
We check the number of lines in the texteditor, and include that information in our PROMPT dialog. Here's the code that asks the user for a line number, then scrolls to that line:
[gotoLine]
timer 0
#1.te "!lines rowcount"
gotoLine=rowcount
msg$="Go to line? Max is ";gotoLine
prompt msg$;gotoLine
if gotoLine>rowcount then gotoLine=rowcount
if gotoLine<1 then gotoLine=1
#1.te "!origin ";gotoLine;" 1"
'or for LB4.02:
#1.te "!origin 1 ";gotoLine 'for LB4.02
#1.te "!setfocus"
TIMER 50, [activateTimer]
wait
Scrolling to a Branchlabel from the Combobox
Eddie now has a combobox that is filled dynamically with the branch labels in the code, as the code is being edited. When the user selects a branch from the combobox, Eddie scrolls the line containing that branch label to the top of the texteditor. The first step in the routine is to get the selection from the combobox. We do that with the SELECTION statement, like this:
#1.c "selection? branchselect$"
The variable "branchselect$" contains the item in the combobox that was selected by the user. If it is "<start>" then we scroll to row 1. If it is something else, then we evaluate it against each line in the texteditor in a loop, until we get to the one that matches. To do this, we retrieve each line, trim it with TRIM$() and see if they are equal. We increment the row counter with each evaluation. When we find the line that is the same as the selection, we scroll the texteditor to the row whose value is in the row counter variable, "i". Once we've found the proper line, there is no need to evaluate further, so we use the EXIT FOR command to exit the loop.
[chooseBranch]
'** CHOOSE BRANCH, SET EDITOR TO THAT POSITION
timer 0
#1.c "selection? branchselect$"
'user selected top of code
If branchselect$="<start>" Then
#1.te "!origin 1 1"
#1.te "!setfocus"
TIMER 50, [activateTimer]
Wait
End If
For i = 1 TO rowcount
#1.te "!line ";i;" line$"
If Trim$(line$)= branchselect$ Then
#1.te "!origin ";i;" 1"
#1.te "!origin 1 ";i 'for LB4.02
#1.te "!setfocus"
exit for
End If
Next i
TIMER 50, [activateTimer]
Wait
There's another way!
Check for future installments of Eddie to find an even easier way to determine the proper line of code that contains a branch label!
DEMO
For the Eddie code, go here: Eddie